package com.ehais.modbuswifi;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.ehais.adapter.StationRecyclerAdapter;
import com.ehais.model.AiStation;
import com.ehais.model.StationInfoResult;
import com.ehais.util.OkHttpCallBack;
import com.ehais.util.OkHttpUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

public class MainStationActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;
    private StationRecyclerAdapter adapter;
    private List<AiStation.AiStationModel> data = new ArrayList<AiStation.AiStationModel>();
    private Integer page = 1;
    private Boolean isMore = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_station);

        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        refreshLayout = (SmartRefreshLayout)findViewById(R.id.refreshLayout);
        refreshLayout.setEnableLoadMore(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //竖直方向
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置item显示方法
        recyclerView.setLayoutManager(layoutManager);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Log.i("refreshLayout","自动加载数据");
                loadData();
            }
        });
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("refreshLayout","加载更多数据");
                        loadData();
                    }
                });
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Log.i("refreshLayout","刷新数据");
                data.clear();
                isMore = true;
                page = 1;
                loadData();
            }
        });

        //触发自动刷新
        refreshLayout.autoRefresh();




    }

    public void loadData(){
        if(isMore == false) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
            return;
        }
        isMore = false;
        Map<String,String> map = new HashMap<String,String>();
        map.put("userId", "3");
        map.put("size", "50");
        map.put("current", page.toString());
        map.put("ticketStatus", "");
        map.put("stationName", "");
        map.put("stationCode", "");
        map.put("longitude", "114.01");
        map.put("latitude", "38.256");
        OkHttpUtil.postEntity("/bai/battery/station/getAiStationByUserId?size=50&current="+page, map, new OkHttpCallBack() {

            @Override
            public void onSuccess(String json) {
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
                Gson gson = new Gson();
                AiStation aiStation = gson.fromJson(json, AiStation.class);

                List<AiStation.AiStationModel> rows = aiStation.getRows();
//                Log.i("MainStationActivity.total",rows.size()+" 条记录 ");
                for(AiStation.AiStationModel row : rows){
//                    Log.i("MainStationActivity.row",row.getStationName());
                    data.add(row);
                }

                if(rows.size() == 10){
                    page ++;
                    isMore = true;
                }

                //将数据源传入到适配器里
                adapter = new StationRecyclerAdapter(data);
                adapter.setOnItemClickListener(new StationRecyclerAdapter.OnItemClickListener() {
                    @Override
                    public void OnClick(int position) {
//                        Toast.makeText(MainStationActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                        loadModel(data.get(position).getId());
                    }
                });


                //显示item
                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setAdapter(adapter);
                    }
                });


//                refreshLayout.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        Log.i("上下拉刷新控件","恢复为正常状态");
//
//                        refreshLayout.finishRefresh(true);
//                    }
//                });
            }
        });
    }

    public void loadModel(Integer stationId){
        OkHttpUtil.get("/bai/battery/station/getStationAndBatteryByStationId?stationId="+stationId, new OkHttpCallBack() {
            @Override
            public void onSuccess(String json) {
                Gson gson = new Gson();
                StationInfoResult aiStation = gson.fromJson(json, StationInfoResult.class);
                if(aiStation != null){
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("STATION",aiStation.getData());

                    Intent intent = new Intent();
                    intent = new Intent(MainStationActivity.this, NewScanActivity.class);
                    intent.putExtras(bundle);

                    if(aiStation.getData().getBatteryCategory().equals("铅酸")){
                        intent.putExtra("category", "Plumbic");
                    }else if(aiStation.getData().getBatteryCategory().equals("铁锂")){
                        intent.putExtra("category", "Phosphoric");
                    }

                    startActivity(intent);
                }


            }
        });
    }
}